package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;
import com.yuwj.chinaa.util.ValidatonCheckUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {


    @BindView(R.id.et_register_phone)
    EditText etPhone;
    @BindView(R.id.tv_send_verification_code)
    TextView tvSendMsgCode;
    @BindView(R.id.et_password)
    EditText etpassword;
    @BindView(R.id.et_register_verification_code)
    EditText etRegisterVerificationCode;
    @BindView(R.id.tv_show_user_agreement)
    TextView tvShowUserAgreement;
    @BindView(R.id.iv_password_eye)
    ImageView ivPasswordEye;
    @BindView(R.id.et_invitation_code)
    EditText etInvitationCode;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    IPresenter presenter;
    int passwordShowType = 0;//0-隐藏，1-开启


    private CountDownTimer timer = new CountDownTimer(1000 * 60, 1000) {
        //参数1，设置倒计时的总时间（毫秒）
        //参数2，设置每次减去多少毫秒
        @Override
        public void onTick(long millisUntilFinished) {
            tvSendMsgCode.setEnabled(false);
            tvSendMsgCode.setText((millisUntilFinished / 1000) + "s");
        }

        @Override
        public void onFinish() {
            tvSendMsgCode.setEnabled(true);
            tvSendMsgCode.setText("发送验证码");
        }
    };

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        return intent;
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_register;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initOperator() {

    }

    @OnClick({R.id.ib_back, R.id.tv_send_verification_code, R.id.iv_password_eye, R.id.btn_register, R.id.tv_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                onBackPressed();
                break;
            case R.id.tv_send_verification_code:
                String phone = etPhone.getText().toString();
                if (TextUtils.isEmpty(phone) || !ValidatonCheckUtil.isMobile(phone)) {
                    ToastUtil.getInstance().shortLength(context, "请输入有效的手机号");
                    return;
                }
                // 发送短信验证码
                JSONObject paramWithSendVerifyCode = new JSONObject();
                try {
                    paramWithSendVerifyCode.put("phone", phone);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.msgGetVerifyCode(paramWithSendVerifyCode.toString());
                timer.start();

                break;
            case R.id.iv_password_eye:
                switch (passwordShowType) {
                    case 0://不可见
                        ivPasswordEye.setImageResource(R.mipmap.ic_password_visible);
                        etpassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);//设置密码可见
                        etpassword.setSelection(etpassword.getText().toString().length());
                        etpassword.requestFocus();
                        passwordShowType = 1;
                        break;
                    case 1://可见
                        ivPasswordEye.setImageResource(R.mipmap.ic_password_invisible);
                        etpassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);//设置密码不可见，如果只设置TYPE_TEXT_VARIATION_PASSWORD则无效
                        etpassword.setSelection(etpassword.getText().toString().length());
                        etpassword.requestFocus();
                        passwordShowType = 0;
                        break;

                }
                break;
            case R.id.btn_register:
                String registerPhone = etPhone.getText().toString();
                String verifyCode = etRegisterVerificationCode.getText().toString();
                String password = etpassword.getText().toString();
                String invitationCode = etInvitationCode.getText().toString();

                if (TextUtils.isEmpty(registerPhone) || !ValidatonCheckUtil.isMobile(registerPhone)) {
                    ToastUtil.getInstance().shortLength(context, "请输入有效的手机号");
                    return;
                }

                if (TextUtils.isEmpty(verifyCode)) {
                    ToastUtil.getInstance().shortLength(context, "请输入验证码");
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    ToastUtil.getInstance().shortLength(context, "请输入新密码");
                    return;
                }
                if (!ValidatonCheckUtil.isLegalPassword(password)) {
                    ToastUtil.getInstance().shortLength(context, "请输入6位及以上字符密码");
                    return;
                }
                if (TextUtils.isEmpty(invitationCode)) {
                    ToastUtil.getInstance().shortLength(context, "请输入邀请码");
                    return;
                }
                //注册
                JSONObject paramWithRegister = new JSONObject();
                try {
                    paramWithRegister.put("uphone", registerPhone);
                    paramWithRegister.put("pcode", verifyCode);
                    paramWithRegister.put("upwd", password);
                    paramWithRegister.put("invcode", invitationCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.userRegister(paramWithRegister.toString());
                break;
            case R.id.tv_login:
                this.finish();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void onMsgGetVerifyCodeSuccess(String code) {
        super.onMsgGetVerifyCodeSuccess(code);
        ToastUtil.getInstance().shortLength(context, "验证码发送成功");
    }

    @Override
    public void onMsgGetVerifyCodeFail(Exception e) {
        super.onMsgGetVerifyCodeFail(e);
        ToastUtil.getInstance().shortLength(context, "" + e.getMessage());
    }

    @Override
    public void onUserRegisterSuccess() {
        super.onUserRegisterSuccess();
        ToastUtil.getInstance().shortLength(context, "注册成功");
        finish();
    }

    @Override
    public void onUserRegisterFail(Exception e) {
        super.onUserRegisterFail(e);
        ToastUtil.getInstance().shortLength(context, "注册失败," + e.getMessage());
    }
}
